﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleDataTools
{
    /// <summary>
    /// 用于接收的数据描述
    /// </summary>
    public class BuffData
    {
        public BuffData() { }
        public BuffData(ItemConfig config)
        {
            FromItemConfig(config);
        }

        public string Name { get; set; }
        public int Count { get; set; }
        public DataType Type { get; set; }
        public DataFormat WriteFunc { get; set; }
        public byte[] Buffs { get; set; }

        public double GetValue(int index)
        {
            double value = 0.0;
            if (Buffs != null && Buffs.Length > index * (Type == DataType.Double ? 8 : 4))
            {
                value = Type == DataType.Double ?
                                BitConverter.ToDouble(Buffs, index * 8) :
                                BitConverter.ToSingle(Buffs, index * 4);
            }
            return value;
        }

        public string GetValueFormmat(int index)
        {
            return string.Format(WriteFunc == DataFormat.F ? "{0:F}" : "{0:E}", GetValue(index));
        }

        public double GetTime()
        {
            return GetValue(0);
        }

        public void FromItemConfig(ItemConfig config)
        {
            if (config != null)
            {
                Name = config.Name;
                Count = config.Count;
                Type = config.Type;
                WriteFunc = config.DataFormat;
            }
        }

    }
}
